<template>
	<view class="contain qxlj_back4">
		<!-- 商品banner -->
		<view class="carousel-section">
			<swiper class="carousel" @change="handleChange" circular indicator-color="rgba(1, 1, 1, .3)"
				indicator-active-color="#fff">
				<swiper-item class="swiper-item" v-for="(item,index) in topBaner" :key="index">
					<view class="image-wrapper">
						<image :src="item" class="loaded" mode="aspectFill" @click="previewImg(item)">
						</image>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<!-- 商品信息 -->
		<view class="shop_box">
			<!-- 名称or价格 -->
			<view class="shop_back">
				<view class="shop_name">
					<view class="name">{{detail.ShopName}}</view>
					<view class="share_box" @click="">
						<image :src="imgUrl + 'upload/img/ws_static/static/yqimg/share.png'" mode=""></image>
						<button id="share" class="f30"
							style="color: #000;font-size: 24rpx;background: none;border:none;padding:0;margin-top: -8px;"
							open-type="share">分享</button>
						<button id="share" class="f30"
							style="background: transparent;border:none;width: 100%;height: 100%;position: absolute;top: 0;left: 0;"
							open-type="share"></button>
					</view>
				</view>
			</view>

			<view class="shop_back person_box">
				<view class="shop_name noShop_name">
					<view class="title">营业时间</view>
					<view class="ck_all">{{detail.BusinessHours}}</view>
				</view>
			</view>
			<view class="shop_back person_box" @click="goPark">
				<view class="shop_name">
					<view class="title">餐厅地址</view>
					<view class="ck_all">{{detail.Address}}</view>
				</view>
			</view>
			<view class="shop_back person_box" @click="goPhone">
				<view class="shop_name">
					<view class="title">餐厅电话</view>
					<view class="ck_all">拨打</view>
				</view>
			</view>

			<!-- 信息 -->
			<!-- 			<view class="shop_back shop_top">
				<view class="" style="padding-top: 2%;">营业时间：{{detail.BusinessHours}}</view>
				<view class="shop_phone shop_name">
					<view class="">
						<view class="name_one">{{detail.ShopName}}</view>
						<view class="name_note" v-if="detail.Address!=null">{{detail.Address}}</view>
					</view>
					<view class="shop_left">
						<view class="img_box" @click="goPark">
							<image src="/static/qxlj/dt.png" mode=""></image>
							<view class="text">{{detail.Distance}}</view>
						</view>
						<view class="border"></view>
						<view class="img_box" @click="goPhone">
							<image src="/static/qxlj/dh.png" mode=""></image>
							<view class="text">电话</view>
						</view>
					</view>
				</view>
			</view> -->

			<!-- 门票及简介 -->
			<view class="shop_back shop_top shop_mp">
				<view class="" style="margin-top: 2%;" v-if="foodList.length>0">
					<view class="title huodong_box">推荐美食</view>
					<view class="">
						<view class="park_item" v-for="(item,index) in foodList" :key="index">
							<view class="right">
								<image :src="item.Img" mode=""></image>
							</view>
							<view class="left">
								<view class="one">{{item.Name}}</view>
								<view class="two" v-if="item.Subtitle!=null">{{item.Subtitle}}</view>
								<view class="three" v-if="item.Address">￥{{item.ShowPrice}}</view>
								<view class="km_box">
									<!-- 									<u-button text="购买" @click="buttonClick(1,item.Id)" shape="circle"
										color="#498FFF"></u-button> -->
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="" style="margin-top: 2%;" v-if="detail.txtSubstance!=null">
					<view class="title huodong_box">商家简介</view>
					<mp-html style="background-color: #fff;" :content="detail.txtSubstance" />
				</view>
			</view>
		</view>
		<spec-popuo ref="refspec" :goodsDetail.sync="goodsDetail" @buy="handleBuy"></spec-popuo>
	</view>
</template>

<script>
	import specPopuo from '../components/spec-popup.vue'
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex'
	import {
		getGoodsDetail,
		getGoodsList,
		getShopBgDetail,
	} from '@/api/mall.js'
	import {
		floatMul
	} from '@/utils/tools.js'
	import Config from "@/config";
	export default {
		components: {
			specPopuo,
		},
		data() {
			return {
				imgUrl: Config.imgUrl,
				detail: {}, //商家详情
				shopID: 0, //商家ID
				current: 0, //banner下标
				topBaner: [], //顶部banner
				foodList: [], //产品数据
				goodsDetail: '',
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.base.userInfo
			}),
		},
		onLoad(e) {
			const _this = this
			console.log(e.id)
			if (e.id) {
				// 商品id
				this.shopID = e.id
				// 加载数据
				this.getBg();

			} else {

			}
			uni.showShareMenu({
				withShareTicket: true,
				//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus: ["shareAppMessage", "shareTimeline"]
			})
		},
		methods: {
			// 处理立即购买
			handleBuy(data) {
				const GoodsInfoList = [{
					BusinessId: this.goodsDetail.BusinessId,
					BusinessName: this.goodsDetail.BusinessModel.CompanyName || this.goodsDetail.Name,
					OrderRemak: '',
					goodsAmount: floatMul(data.number * 1, data.Price),
					ProductInfo: [{
						Num: data.number,
						...data
					}],
					glist: [{
						Num: data.number,
						...data
					}]
				}]
				this.$refs.refspec.hide()
				this.setGoodsInfoList(GoodsInfoList)
				uni.navigateTo({
					url: '/mall_subpages/create-order/create-order?id=' + this.detail.BusinessId
				})
			},
			//获取全部数据
			async getAllDate() {
				uni.showLoading({});
				try {
					const Data = await getGoodsList({
						BusinessId: this.shopID,
					})
					uni.hideLoading();
					console.log(Data.Data)
					this.foodList = Data.Data;
				} catch (e) {

				}
			},
			async getBg() {
				try {
					uni.showLoading({
						title: '加载中...',
						mask: true
					})
					const {
						Data
					} = await getShopBgDetail({
						Id: this.shopID
					})
					console.log(Data)
					if (Data.LatitudeAndLongitude != null) {
						let JL = await this.GetDistance(Data.LatitudeAndLongitude.split(',')[0], Data
							.LatitudeAndLongitude.split(',')[1]);
						if (JL < 1) {
							Data.Distance = JL * 1000 + 'M'
						} else {
							Data.Distance = JL + 'KM'
						}
					} else {
						Data.Distance = '未知'
					}
					this.detail = Data;
					this.topBaner = Data.BusinesImgList.split('|');
					uni.setNavigationBarTitle({
						title: Data.ShopName
					})
					uni.hideLoading()
					this.getAllDate();
				} catch (e) {
					//TODO handle the exception
				}
			},
			//导航去
			goPark() {
				if (this.detail.LatitudeAndLongitude != null) {
					uni.openLocation({
						latitude: parseFloat(this.detail.LatitudeAndLongitude.split(',')[0]),
						longitude: parseFloat(this.detail.LatitudeAndLongitude.split(',')[1]),
						success: function(res) {
							console.log('打开系统位置地图成功')
						},
						fail: function(error) {
							console.log(error)
						}
					})
				}
			},
			//计算距离
			getSelfLocation() {
				let location = {
					lat: 0, //维度
					lng: 0, //经度
				}
				return new Promise((resolve, reject) => {
					uni.getLocation({
						type: 'gcj02', // 使用国标坐标系
						isHighAccuracy: true, // 高精度定位，会调用gps获取高精度坐标
						success(res) {
							location.lat = res.latitude
							location.lng = res.longitude,
								resolve(location);
						},
						fail(err) {
							console.log(err)
							reject(location);
						}
					})
				})
			},
			//进行经纬度转换为距离的计算
			Rad(d) {
				return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
			},
			//计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
			async GetDistance(lat1, lng1) {
				uni.showLoading({});
				const res = await this.getSelfLocation()
				let lat2 = res.lat; //维度
				let lng2 = res.lng; //经度
				var radLat1 = this.Rad(lat1);
				var radLat2 = this.Rad(lat2);
				var a = radLat1 - radLat2;
				var b = this.Rad(lng1) - this.Rad(lng2);
				var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
					Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
				s = s * 6378.137; // EARTH_RADIUS;
				s = Math.round(s * 10000) / 10000; //输出为公里
				let newJL = s.toFixed(2);
				uni.hideLoading();
				return newJL;
			},
			buttonClick(e, goodsID) {
				this.lodaData(goodsID)
				this.$refs.refspec.show(e)
			},
			previewImg(e) { //图片预览
				uni.previewImage({
					urls: [e],
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			//进入评论列表
			goEvtion() {
				uni.navigateTo({
					url: `/mall_subpages/evaluation/evaluation-list?Id=${this.detail.Id}`
				})
			},
			...mapMutations(['setGoodsInfo', 'setGoodsInfoList']),
			//拨打商家电话
			goPhone() {
				uni.makePhoneCall({
					phoneNumber: this.detail.Mobile,
					success: (result) => {},
					fail: () => {},
					complete: () => {}
				});
			},
			async lodaData(goodsID) {
				try {
					uni.showLoading({
						title: '加载中...',
						mask: true
					})
					const {
						Data
					} = await getGoodsDetail({
						Id: goodsID
					})
					this.goodsDetail = Data;
					this.$nextTick(() => {
						this.$refs.refspec.loadData()
					})
					uni.hideLoading()
				} catch (e) {
					console.log(e)
				}
			},
			// 商品banner改变
			handleChange(e) {
				this.current = e.detail.current
			},
		},
	}
</script>

<style scoped lang="scss">
	.carousel-section {
		// position: relative;
		height: 500rpx;
		background-color: #e9e9e9;

		/* 轮播图 */
		/deep/ .uni-swiper-dot {
			width: 20rpx;
			height: 10rpx;
			border-radius: 50rpx;
		}
	}

	.carousel-section /deep/ .uni-swiper-dots-horizontal {
		bottom: 30rpx;

		.uni-swiper-dot {}

		.uni-swiper-dot-active {
			background-color: #e07b2b !important;
		}
	}

	.carousel {
		// position: relative;
		// left: 10%;
		// bottom: -10%;
		width: 100%;
		height: 500rpx;
		box-shadow: 0 0 20rpx #b4b4b4;
		z-index: 9;

		.swiper-item {}
	}

	.dot-desc {
		position: absolute;
		bottom: 20rpx;
		right: 20rpx;
		background: rgba(#333, 0.3);
		padding: 4rpx 16rpx;
		border-radius: 10rpx;
		color: #fff;
		font-size: $uni-font-size-sm - 4rpx;
	}

	.image-wrapper {
		height: 100%;
		width: 100%;
		background: #ccc;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.shop_box {
		// padding: 3%;
		position: relative;
		margin-top: -20px;
		border-radius: 18px 18px 0 0;
		padding-bottom: 80px;
		background-color: #fff;
		padding: 2% 6%;


		.shop_back {
			background-color: #fff;
		}

		.shop_name {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 2% 0;

			.name {
				font-weight: bold;
				color: #000;
				font-size: 18px;
			}

			.share_box {
				position: relative;
				text-align: center;

				image {
					width: 20px;
					height: 20px;
				}

				view {
					font-size: 14px;
				}
			}

		}

		.shop_top {
			background-color: #fff;
			margin-top: 4%;
		}

		.shop_phone {
			width: 96%;
			padding: 0;

			.name_one {
				color: #000;
				font-size: 16px;
			}

			.name_note {
				font-size: 12px;
				color: #ccc;
				margin-top: 2%;
			}

			.shop_left {
				width: 35%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #ccc;

				.border {
					width: .5px;
					height: 35px;
					background-color: #ccc;
				}

				.img_box {
					text-align: center;
				}

				image {
					width: 20px;
					height: 20px;
				}

				.text {
					font-size: 12px;
				}
			}
		}

	}

	.shop_mp {
		margin-top: 4%;
		padding-top: 2%;

		.title {
			width: 100%;
			// padding-left: 2%;
			font-size: 18px;
		}
	}


	#share::after {
		border: none !important;
		height: 0 !important;
	}

	.collection {
		color: #498FFF;
	}

	//线上产品
	.shop_cpxq {
		.name {
			font-weight: normal;
		}

		.symbol {
			font-size: $uni-font-size-sm + 10rpx;
			color: rgb(248, 106, 33);
		}

		.price {
			color: rgb(248, 106, 33);
			font-size: 46rpx;
			font-weight: 600;
		}

		.oldPrice {
			font-size: $uni-font-size-sm;
			padding-left: 10rpx;
			color: #999;
			text-decoration: line-through;
		}
	}

	.person_box {
		// height: 140rpx;
		margin-top: 4%;

		.shop_name {
			// height: 140rpx;
			position: relative;

			.ck_all {
				margin-right: 5%;
			}

			.title {
				font-weight: bold;
			}
		}

		.shop_name::after {
			content: " ";
			display: inline-block;
			height: 8px;
			width: 8px;
			border-width: 2px 2px 0 0;
			border-color: rgb(149, 155, 167);
			border-style: solid;
			-webkit-transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
			transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
			position: absolute;
			right: 0px;
		}

		.noShop_name::after {
			display: none;
		}
	}

	.shop_car {
		position: relative;

		.car_num {
			background-color: red;
			color: #fff;
			width: 16px;
			height: 16px;
			line-height: 16px;
			position: absolute;
			top: -6px;
			right: 0;
			border-radius: 50%;
		}
	}

	.bottom_icon {
		margin-top: 2%;
	}


	.park_item {
		width: 100%;
		height: 200rpx;
		border-radius: 2%;
		display: flex;
		justify-content: space-between;
		padding: 3% 2%;
		margin-top: 2%;

		.left {
			width: 65%;
			position: relative;

			.one {
				color: #000;
				font-size: 36rpx;

			}

			.two {
				color: #999999;
				// height: 60rpx;
				margin-top: 2%;
				font-size: 24rpx;
			}

			.three {
				font-size: 24rpx;
				margin-top: 2%;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				color: #498FFF;
				font-size: 36rpx;
			}

			.km_box {
				color: #fff;
				font-size: 20rpx;
				position: absolute;
				top: 25%;
				right: 0;
				width: 140rpx;
			}
		}

		.right {
			width: 30%;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
				border-radius: 7px;
			}
		}

	}

	.huodong_box {
		font-size: 36rpx;
	}
</style>